import Vue from 'vue'
import VueRouter from 'vue-router'
//导入注册页面组件
import Login from '../components/Login.vue'
//导入home主页面组件
import Home from '../components/Home.vue'
//导入welcom页面组件
import Welcome from '../components/Welcome.vue'
//导入用户列表页面组件
import Users from '../components/user/Users.vue'
//导入权限列表组件页面
import Rights from '../components/power/Rights.vue'
//导入角色列表页面组件
import Roles from '../components/power/Roles.vue'
//导入商品列表页面组件
import Cate from '../components/goods/Cate.vue'
//导入分类参数页面组件
import Params from '../components/goods/Params.vue'
//导入商品列表页面组件
import List from '../components/goods/List.vue'
//导入添加商品页面组件
import Add from '../components/goods/Add.vue'
//引入订单页面组件
import Order from '../components/order/Order.vue'
//导入数据报表页面组件
import Report from '../components/report/Report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
    ]
  }
]


const router = new VueRouter({
  routes
})
//使用路由导航守卫判断访问的页面是否有权限，没有权限就跳转到登录页面
//to是将要访问的页面，from代表从哪个页面跳转来的，next是指定放行页面
router.beforeEach((to, from, next) => {
  //如果访问登录页就放行
  if (to.path === '/login') return next()
  //访问其他页面就检查token
  const tokenStr = window.sessionStorage.getItem('token')
  //如果没有token就强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})
export default router
